package com.gupao.vip.nio.buffer;

import java.nio.ByteBuffer;

/**
 * 只读缓冲区
 * @创建人 majingang
 * @联系方式 aoyouxingkong08@163.com
 * @创建日期 2020-04-15
 * @描述
 */
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);

        //缓冲区中的数据0-9
        for(int i=0; i<byteBuffer.capacity(); ++i){
            byteBuffer.put((byte)i);
        }

        //创建子缓冲区
        byteBuffer.position(3);
        byteBuffer.limit(7);

        //创建只读缓冲区
        ByteBuffer readBuffer = byteBuffer.asReadOnlyBuffer();
        //改变子缓冲区的内容
        for(int i=0; i<readBuffer.capacity(); ++i){
            byte b = readBuffer.get(i);
            b *= 10;

            //写入失败，无法写入的
            readBuffer.put(b);
        }
        byteBuffer.position(0);
        byteBuffer.limit(byteBuffer.capacity());
        while(byteBuffer.hasRemaining()){
            System.out.println(byteBuffer.get());
        }
    }
}
